package com.shop.zkmall.app.controller.a.live;


import java.security.MessageDigest;

public class LiveUrlUtil {

    public static String buildPushUrl(String pushDomain, String streamName, String txSecret, long validSeconds) {
        long txTime = (System.currentTimeMillis() / 1000) + validSeconds;

        String input = txSecret + streamName + Long.toHexString(txTime).toUpperCase();
        String txSecretHash = md5(input);

        return "rtmp://" + pushDomain + "/live/" + streamName +
                "?txSecret=" + txSecretHash +
                "&txTime=" + Long.toHexString(txTime).toUpperCase();
    }

    private static String md5(String src) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] array = md.digest(src.getBytes("UTF-8"));
            StringBuilder sb = new StringBuilder();
            for (byte item : array) {
                sb.append(String.format("%02x", item));
            }
            return sb.toString();
        } catch (Exception e) {
            return null;
        }
    }
}
